package cn.htsyb.synergy.tester.config;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.EnvironmentConfiguration;
import org.apache.commons.configuration.XMLConfiguration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.text.MessageFormat;

public class InitConfig {
    protected static XMLConfiguration synertestConfig;
    protected static EnvironmentConfiguration envConfig = new EnvironmentConfiguration();
    private static final Log defLog = LogFactory.getLog(InitConfig.class);
    private static final String synertestConfigPath = "synertest.xml";
    private static String workspacePath;
    private static String localCoding;
    private static String language;

    static {
        if (synertestConfig == null) {
            try {
                synertestConfig = new XMLConfiguration(synertestConfigPath);
            } catch (ConfigurationException e) {
                defLog.error(MessageFormat.format("无法加载{0}， 请检查classpath下是否存在", synertestConfigPath));
            }
        }
    }

    public static String getWorkspacePath() {
        workspacePath = workspacePath == null ? synertestConfig.getString("workspace[@path]").trim() : workspacePath;
        return workspacePath;
    }

    public static String getLocalCoding() {
        localCoding = localCoding == null ? synertestConfig.getString("locale[@coding]").trim() : localCoding;
        return localCoding;
    }

    public static String getLanguage() {
        language = language == null ? synertestConfig.getString("locale[@language]").trim() : language;
        return language;
    }

    public static String getEnvProperty(String propertyKey) {
        return envConfig.getString(propertyKey).trim();
    }
}
